package com.api.gbhg.liuwenbo.commons.entitis;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 通用返回结果体
 * @Author lwb
 * @Date 2023.4.13
 */
public class CommonResult<T> {

    private Integer code;
    private String message;
    private T data;

    @Override
    public String toString() {
        return "{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    public CommonResult() {
    }

    public CommonResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public CommonResult(StatusMsg msg) {
        this.code = msg.getCode();
        this.message = msg.getMessage();
    }

    public CommonResult(StatusMsg msg,T data) {
        this.code = msg.getCode();
        this.message = msg.getMessage();
        this.data = data;
    }

    public CommonResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public String toJSONString(){
        Map<String,Object> map = new HashMap<>();
        map.put("code",this.code);
        map.put("message",this.message);
        if (this.data!=null)
            map.put("data",this.data);
        return JSON.toJSONString(map);
    }

}
